<!-- 发布求助页面 -->
<template>
  <div class="post-help">
    <navigationBar></navigationBar>
    <div class="over">
      <h1>
        <img src="../img/xunzhang.png" /><span
          style="display: inline-block; vertical-align: middle"
          >{{ helpId ? "更新" : "发布" }}求助</span
        >
      </h1>
    </div>

    <div class="aa">
      <el-form
        :model="form"
        ref="publishForm"
        label-width="80px"
        size="normal"
        :rules="rules"
      >
        <ul>
          <el-form-item label="" size="normal" prop="categoryId">
            <li>
              求助类别:
              <el-select v-model="form.categoryId" placeholder="请选择">
                <el-option
                  v-for="item in categoryList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </li>
          </el-form-item>

          <el-form-item label="" size="normal" prop="title">
            <li>
              <p>标题：</p>
              <el-input
                v-model="form.title"
                placeholder="请输入标题"
                maxlength="15"
                show-word-limit
              ></el-input>
            </li>
          </el-form-item>
          <el-form-item label="" size="normal">
            <li>
              <h4>地点：</h4>
              <el-input
                v-model="form.location"
                placeholder="请输入地点"
              ></el-input>
            </li>
          </el-form-item>

          <el-form-item label="" size="normal" prop="needNum">
            <li>
              <h4>所需人数:</h4>
              <!-- <el-input
                v-model.number="form.needNum"
                placeholder="所需人数"
              ></el-input> -->
              <el-input-number
                v-model="form.needNum"
                size="normal"
                :min="1"
                :step="1"
                :controls="false"
                placeholder="所需人数"
              >
              </el-input-number>
            </li>
          </el-form-item>

          <el-form-item label="" size="normal" prop="reward">
            <li>
              <h4>报酬：</h4>
              <!-- <el-input
                v-model.number="form.reward"
                placeholder="报酬"
              ></el-input> -->
              <el-input-number
                v-model="form.reward"
                size="normal"
                :min="0"
                :step="1"
                :controls="false"
                placeholder="报酬"
              >
              </el-input-number>
            </li>
          </el-form-item>
          <el-form-item label="">
            <li>
              <p>详细信息：</p>
              <el-input
                type="textarea"
                placeholder="详细信息"
                v-model="form.content"
                maxlength="200"
                show-word-limit
                rows="5"
              >
              </el-input>
            </li>
          </el-form-item>
          <el-form-item label="" size="normal" style="text-align: center">
            <el-button type="primary" size="default" @click="submit()">
              {{ helpId ? "更新" : "发布" }}
            </el-button>
          </el-form-item>
        </ul>
      </el-form>
    </div>
  </div>
</template>

<script>
import navigationBar from "@/components/navigationBar.vue";
import { getAllCategory } from "@/api/category";
import { addHelp, getHelpDetail, updateHelp } from "@/api/help";
export default {
  name: "postHelp",
  components: { navigationBar },
  data() {
    return {
      helpId: null,
      categoryList: [],
      form: {
        title: null,
        reward: null,
        categoryId: null,
        needNum: null,
        content: null,
      },
      rules: {
        needNum: [
          { type: "number", message: "人数必须为数字值" },
          { required: true, message: "人数不能为空", trigger: "blur" },
        ],
        reward: [
          { type: "number", required: true, message: "报酬必须为数字值" },
          { required: true, message: "报酬不能为空" },
        ],
        categoryId: [{ required: true, message: "分类不能为空" }],
        title: [{ required: true, message: "标题不能为空" }],
      },
    };
  },
  methods: {
    getCategoryList() {
      getAllCategory().then((res) => {
        this.categoryList = res;
        console.log(res);
      });
    },
    submit() {
      this.$refs.publishForm.validate((valid) => {
        if (valid) {
          console.log(this.form);
          if (this.helpId) {
            updateHelp(this.form).then((res) => {
              this.$router.back();
              this.$notify({
                title: "成功",
                message: (this.helpId ? "更新" : "发布") + "成功",
                type: "success",
              });
            });
          } else {
            addHelp(this.form).then((res) => {
              this.$router.back();
              this.$notify({
                title: "成功",
                message: (this.helpId ? "更新" : "发布") + "成功",
                type: "success",
              });
            });
          }
        } else {
          console.log("error submit!!");
          console.log(this.form);
          return false;
        }
      });
    },
    getInitData() {
      this.getCategoryList();
      let helpId = this.$route.query.helpId;
      this.helpId = helpId;
      if (helpId) {
        getHelpDetail(helpId).then((res) => {
          this.form = res;
          console.log(res);
        });
      }
    },
  },
  created() {
    this.getInitData();
  },
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
.post-help {
  background: white;
}
.over {
  height: 150px;
  background-color: rgb(167, 225, 248);
  margin-bottom: 50px;
  width: 100%;
  min-width: 1192px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-content: center;
}
h1 {
  text-align: center;
  letter-spacing: 20px;
  color: white;
  font-size: 40px;
}
.over img {
  width: 40px;
  height: 40px;
  vertical-align: middle;
  margin-right: 10px;
}
.aa span {
  font-size: 50px;
  color: rgb(163, 220, 243);
}
.fabu {
  position: relative;
  left: 100px;
}
li {
  list-style: none;
  color: skyblue;
}
input {
  border: 1px solid skyblue;
  width: 500px;
  height: 40px;
  border-radius: 5px;
  width: 100%;
}
button {
  background-color: skyblue;
  border-radius: 5px;
  width: 120px;
  height: 40px;
  margin-right: 50px;
  border: 1px solid skyblue;
  color: white;
}
.but {
  text-align: center;
  position: relative;
  width: 100%;
}
.duigou {
  width: 30px;
  height: 30px;
}
button:hover {
  background-color: rgb(157, 212, 234);
}

select {
  border: 1px solid skyblue;
  width: 500px;
  height: 40px;
  border-radius: 20px;
}
.el-input {
  width: 500px;
}
.aa {
  margin: 0px auto;
  width: fit-content;
}
.el-input-number {
  width: 500px;
}
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
</style>